package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.ResultInfo;
import com.kbbywyclient.data.model.BoughtTableModel;
import com.kbbywyclient.data.model.LoansBuyModel;
import com.kbbywyclient.data.model.LoansTableMenuModel;
import com.kbbywyclient.data.model.StatusUpdateModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.BoughtTableContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.customservice.ApiException;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observer;

/**
 * Description:
 * Created by FHW
 * on 2017/4/12
 */

public class BoughtTablePresenter extends BasePresenter implements BoughtTableContract.Presenter {

    private BoughtTableContract.View view;
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    private int pageno = 1;
    private List<ResultInfo> resultInfoList = null;

    @Inject
    public BoughtTablePresenter(BoughtTableContract.View view) {
        this.view = view;
        resultInfoList = new ArrayList<>();
        this.view.setAdapterOrNotify(resultInfoList);
    }

    @Override
    public void getLoansBuyOrders(boolean isClean, String SalesID, String LoansType, String WorkTypeList, String ProductLoansID
            , String KeyWord, String Status, long ApplyStarDateTime, long ApplyEndDateTime) {

        rsaJsonUtil.put("SalesID", SalesID);
        if (isClean) {
            pageno = 1;
        }
        rsaJsonUtil.put("pageno", pageno + "");
        rsaJsonUtil.put("pageSize", API.PAGE_SIZE);
        if (!LoansType.equals("0")) {
            rsaJsonUtil.put("LoansType", LoansType);
        }
        if (StringUtil.isNotEmpty(WorkTypeList)) {
            rsaJsonUtil.put("WorkTypeList", WorkTypeList);
        }
        if (!ProductLoansID.equals("0")) {
            rsaJsonUtil.put("ProductLoansID", ProductLoansID);
        }
        if (StringUtil.isNotEmpty(KeyWord)) {
            rsaJsonUtil.put("KeyWord", KeyWord);
        }
        if (StringUtil.isNotEmpty(Status)) {
            rsaJsonUtil.put("Status", Status);
        }
        if (ApplyStarDateTime != 0) {
            rsaJsonUtil.put("ApplyStarDateTime", ApplyStarDateTime);
        }
        if (ApplyEndDateTime != 0) {
            rsaJsonUtil.put("ApplyEndDateTime", ApplyEndDateTime);
        }

        ApiServiceHelper.
                appointThreadNormal(apiService.getLoansBuyOrders(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<BoughtTableModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            view.loadFaiBoughtTable(e.getMessage());
                        } else {
                            HttpExcUtil.printError(e);
                        }
                    }

                    @Override
                    public void onNext(BoughtTableModel boughtTableModel) {

                        if (isClean) {
                            resultInfoList.clear();
                        }
                        resultInfoList.addAll(boughtTableModel.getData().getResult());
                        view.setAdapterOrNotify(resultInfoList);//刷新
                        view.loadSucBoughtTable(resultInfoList);
                        //分页
                        if (boughtTableModel.getData().getPaging().getCurrentPage() >= boughtTableModel.getData().getPaging().getTotalPage()) {
                            if (boughtTableModel.getData().getResult().size() > 1) {
                                view.setNoMore();
                            }
                        } else {
                            pageno++;
                            view.setMoreComplete();
                        }

                    }
                });
    }

    @Override
    public void getloansOrdersMenu(String SalesID) {
        rsaJsonUtil.put("SalesID", SalesID);
//        view.showLoadingDialog(mContext.getString(R.string.text_loading));

        ApiServiceHelper.
                appointThreadNormal(apiService.getloansOrdersMenu(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<LoansTableMenuModel>() {
                    @Override
                    public void onCompleted() {
//                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
//                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(LoansTableMenuModel loansTableMenuModel) {
                        view.loadSucMenu(loansTableMenuModel.getData());
                    }
                });
    }

    @Override
    public void getApplyLoansBuy(String SalesID, String ApplyLoansID, String ApplyLoansSN, String Monopolize, String BuyType) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("ApplyLoansID", ApplyLoansID);
        rsaJsonUtil.put("ApplyLoansSN", ApplyLoansSN);
        if (StringUtil.isNotEmpty(Monopolize)) {
            rsaJsonUtil.put("Monopolize", Monopolize);
        }
        if (StringUtil.isNotEmpty(BuyType)) {
            view.showLoadingDialog(mContext.getString(R.string.text_loading));
            rsaJsonUtil.put("BuyType", BuyType);
        }

        ApiServiceHelper.
                appointThreadNormal(apiService.getApplyLoansBuy(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<LoansBuyModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        if (e instanceof ApiException) {
                            view.loadFaiTableBuy(e.getMessage());
                        } else {
                            HttpExcUtil.printError(e);
                        }
                    }

                    @Override
                    public void onNext(LoansBuyModel loansBuyModel) {
                        view.loadSucTableBuy(loansBuyModel.getData(), BuyType);
                    }
                });
    }

    @Override
    public void getApplyLoansUpdate(String SalesID, String ApplyLoansID, String ApplyLoansSN, String Status, String SuccessDescription
            , String SuccessLoansTerm, String SuccessLoansMoney, String MonthlyInterestRate) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("ApplyLoansID", ApplyLoansID);
        rsaJsonUtil.put("ApplyLoansSN", ApplyLoansSN);
        if (StringUtil.isNotEmpty(Status)) {
            rsaJsonUtil.put("Status", Status);
        }
        if (StringUtil.isNotEmpty(SuccessDescription)) {
            rsaJsonUtil.put("SuccessDescription", SuccessDescription);
        }
        if (StringUtil.isNotEmpty(SuccessLoansTerm)) {
            rsaJsonUtil.put("SuccessLoansTerm", SuccessLoansTerm);
        }
        if (StringUtil.isNotEmpty(SuccessLoansMoney)) {
            rsaJsonUtil.put("SuccessLoansMoney", SuccessLoansMoney);
        }
        if (StringUtil.isNotEmpty(MonthlyInterestRate)) {
            rsaJsonUtil.put("MonthlyInterestRate", MonthlyInterestRate);
        }
        view.showLoadingDialog(mContext.getString(R.string.text_loading));

        ApiServiceHelper.
                appointThreadNormal(apiService.getApplyLoansUpdate(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<StatusUpdateModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(StatusUpdateModel statusUpdateModel) {
                        view.loadSucLoansUpdate(statusUpdateModel.getData());
                    }
                });

    }

}
